package com.tyj.movie.util;

import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayTradePagePayRequest;
import com.tyj.movie.vo.PayVo;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

@ConfigurationProperties(prefix = "alipay")
@Component
@Data
public class AlipayTemplate {

    //在支付宝创建的应用的id
    private   String app_id = "2021000117627848";

    // 商户私钥，您的PKCS8格式RSA2私钥
    private  String merchant_private_key ="MIIEvwIBADANBgkqhkiG9w0BAQEFAASCBKkwggSlAgEAAoIBAQCfHb4ses7eCFUI2xmMvHNZCN2c0fd6IqCR3cHD2wMnL84iwTmQ3nadNoEEd4xMauudyudoGoFe7xdoxAoeBJGoOvwxLAInJgxR3h8gVPuItcfDrTL1iNGJxcQ5GQ3kcWc0MYnSS69ck4lRqdJCUaaI/UMGoWVJUYqNY9KCsZi4PZ1pJ+8/Jpjor8be2NhNoAgi/3ZAkS4j5JPB4ZeOsHp7oC7Rjl5jfy5AZzU0L1dRhTsPfUm/lNc9qpgEM6Ine7t098W8SX1MzOagYTH1Yos6OrTWeeC7RNSD3MbYP+eWG6viPxM3Ez2IgLE6HQlVF+bH5qwQOQny9V2SQkDLK2nbAgMBAAECggEAWLSEKNfuK+oJPf6np8YHqENBCQQlK3nSen9eXonioB4OlWGTbigXiF2yX4MmPBP/duB/UZI1PW+dZVKlMUbqzRbVMbSPHrQfeiSSxbua3wQ4ceZE2o2EA4K1ef91tz1hg796TjrTcJDvpQ8/oKlUZlqK209u4AkNbPWRjjrkIRGTnTX9eKX/4/cuPXbcI9RYgMzROG2qiGBMzFhfKIozyvJx2oLmlW53D0F9KL1mHDJRF66IBkpuYo/wNoesqAQy3cjM+TDH92EvAsc8P5pUSkv6Xe6jo70x1e4GBZrfts5AU5XdhkAcSccsVkKhvZTsyBsYkN5SPCqvKPqB3W2YAQKBgQDy9hJAmqivu+8pBZd8NeC6huz+KDtNw4ebKPEi8M8kjo8R88hMe7R3te0cTcPRootsK2KmGqxTINZ+BaYzdRwzpvI8jKPNg42fY9jdJEHpfoXqNJfV4eKqdo6T3/4Y9Be/s5mK/ueqB6OEeAIuJjsg38U+yKP/4hBXhsy7DOYMgQKBgQCnp8O2RoVAC5MkH0mWppH7HiMMCqp/aouSbxYn0OFOJ4s4sMjryUNPeRik2iBOLbYrbDTf7MCvS6VG2TiZlJoy+xBHa6Qb6es1RXUci+kN/2r9y+qp6iNJ0Bhb+eBY9o85PndynBNXytTWAQ+lM4Y1a2kdmULXiyuvTZdqVUj4WwKBgQCBep3zi/iF3tPvuqkmatgez7URJYOlR6THDaSkiEz8IHUijhaam/prKar5QIAxAjznIp/qAaEAlCl686vFFI45AKlrl0/cQFWDMgTqpV3y/5iCXhDkzQKs8Y2+RMr3pW3ane4f4wS22mdMO2B+E6XDOT223dp4jZND8fS9YnxfAQKBgQCK5hrnMJXE8dH94038WhQnkFhI6a1H0uW/c/lPE2cWcPyPAyfy8NOrTBB2w6BwJL8ASq2jt2oL0QglImIaG1uFUSuzgCJ+XdZYVCsqc1+qESp7Ci5oh2qlBJqp2ONd0SfnrTEY6uW7nnemtUpY+879AvgbZZZTLSIMIqSHnBDQcQKBgQCY1/F7h0qGakP5oe8ijH2WkkXLZtu9KsqslLqRTBmI4Y8fwy6TGcLKL7OaPtG0V/Lx5Ymii8BAW9KT/s3DB9/XWcR4ZS6+MKgPtBYNNyutNnByJmSjto2XHP5gyhk9Lf18dUz6nsu7x5SHnEovAewEeyHRqoDEnS4LSfF6jW62FQ==";
    // 支付宝公钥,查看地址：https://openhome.alipay.com/platform/keyManage.htm 对应APPID下的支付宝公钥。
    private  String alipay_public_key ="MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEA22XOpS+KuZYCwH9/1BCM/E5HES1wyZYnR1i4+v1TlP3R8Kt0QnZTbXoHfmaPN+SWAHyC+W9PxQddn54d6iLRqy8IYliWu/7yvjQU6KF9At+Vouqo+Qa579t9+VvnCwxOS+CkwM8R/ai30XVTUM8JMURo3eWU3aKOZjfj7r1MIYXA85P/6ZD5rbl82K+kW2eBORaWiffZMrSg8oyJterBi4YU2udvKdQFWbpuo6WEH5u34L30/fMgBo5vBIxvTyBqbIC4LHYKQcu0WJ49sRjBs8KDrTZCWdrHAC9+67IYL6noh4KaaCh/w6r+mQcA9MglrD7kp2y3szolKXPoR5VKuwIDAQAB";

    // 服务器[异步通知]页面路径  需http://格式的完整路径，不能加?id=123这类自定义参数，必须外网可以正常访问
    // 支付宝会悄悄的给我们发送一个请求，告诉我们支付成功的信息
    private  String notify_url="http://41246y27o9.wicp.vip/payed/notify";

    // 页面跳转同步通知页面路径 需http://格式的完整路径，不能加?id=123这类自定义参数，必须外网可以正常访问
    //同步通知，支付成功，一般跳转到成功页
    private  String return_url="http://www.catdogtv.com/paySuccess";

    // 签名方式
    private  String sign_type = "RSA2";

    // 字符编码格式
    private  String charset = "utf-8";

    // 支付宝网关； https://openapi.alipaydev.com/gateway.do
    private  String gatewayUrl = "https://openapi.alipaydev.com/gateway.do";

    public  String pay(PayVo vo) throws AlipayApiException {

        //AlipayClient alipayClient = new DefaultAlipayClient(AlipayTemplate.gatewayUrl, AlipayTemplate.app_id, AlipayTemplate.merchant_private_key, "json", AlipayTemplate.charset, AlipayTemplate.alipay_public_key, AlipayTemplate.sign_type);
        //1、根据支付宝的配置生成一个支付客户端
        AlipayClient alipayClient = new DefaultAlipayClient(gatewayUrl,
                app_id, merchant_private_key, "json",
                charset, alipay_public_key, sign_type);

        //2、创建一个支付请求 //设置请求参数
        AlipayTradePagePayRequest alipayRequest = new AlipayTradePagePayRequest();
        alipayRequest.setReturnUrl(return_url);
        alipayRequest.setNotifyUrl(notify_url);

        //商户订单号，商户网站订单系统中唯一订单号，必填
        String out_trade_no = vo.getOut_trade_no();
        //付款金额，必填
        String total_amount = vo.getTotal_amount();
        //订单名称，必填
        String subject = vo.getSubject();
        //商品描述，可空
        String body = vo.getBody();

        alipayRequest.setBizContent("{\"out_trade_no\":\""+ out_trade_no +"\","
                + "\"total_amount\":\""+ total_amount +"\","
                + "\"subject\":\""+ subject +"\","
                + "\"body\":\""+ body +"\","
                + "\"timeout_express\":\"1m\","  //订单支付未超过一分钟 支付超时
                + "\"product_code\":\"FAST_INSTANT_TRADE_PAY\"}");

        String result = alipayClient.pageExecute(alipayRequest).getBody();

        //会收到支付宝的响应，响应的是一个页面，只要浏览器显示这个页面，就会自动来到支付宝的收银台页面
        System.out.println("支付宝的响应："+result);

        return result;

    }
}
